package com.example.demo2.mapper;

import com.example.demo2.entity.Program;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProgramMapper {
    @Select("select * from program where pjId =#{pjId} and pgType=#{pgType}")
    public List<Program> findProgramByPjId(@Param("pjId") Integer pjId, @Param("pgType") String pgType);


    @Select("select program.pgUrl from program where pgId=#{pgId}")
    String findPgUrlByPgId(@Param("pgId") Integer pgId);

    @Select("SELECT * FROM program WHERE pjId = #{pjId}")
    @Results({
            @Result(property = "pgId", column = "pgId"),
            @Result(property = "pgName", column = "pgName"),
            @Result(property = "pgUrl", column = "pgUrl"),
            @Result(property = "pgType", column = "pgType"),
            @Result(property = "pjId", column = "pjId"),
            @Result(property = "updateTime", column = "updateTime")
    })
    List<Program> selectProgramsByPjId(@Param("pjId") Integer pjId);

    @Insert("INSERT INTO program (pgName, pgUrl, pgType, pjId, updateTime) " +
            "VALUES (#{pgName}, #{pgUrl}, #{pgType}, #{pjId}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "pgId")
    int insertProgram(Program program);

    @Update("UPDATE program SET " +
            "pgName = #{pgName}, " +
            "pgUrl = #{pgUrl}, " +
            "pgType = #{pgType}, " +
            "pjId = #{pjId}, " +
            "updateTime = #{updateTime} " +
            "WHERE pgId = #{pgId}")
    @Results({
            @Result(property = "pgId", column = "pgId"),
            @Result(property = "pgName", column = "pgName"),
            @Result(property = "pgUrl", column = "pgUrl"),
            @Result(property = "pgType", column = "pgType"),
            @Result(property = "pjId", column = "pjId"),
            @Result(property = "updateTime", column = "updateTime")
    })
    int updateProgram(Program program);

    @Select("SELECT * FROM program WHERE pgId = #{pgId}")
    @Results({
            @Result(property = "pgId", column = "pgId"),
            @Result(property = "pgName", column = "pgName"),
            @Result(property = "pgUrl", column = "pgUrl"),
            @Result(property = "pgType", column = "pgType"),
            @Result(property = "pjId", column = "pjId"),
            @Result(property = "updateTime", column = "updateTime")
    })
    Program selectProgramById(@Param("pgId") int pgId);

    @Delete("DELETE FROM program WHERE pgId = #{pgId}")
    int deleteProgram(@Param("pgId") int pgId);
}